<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .send-data-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>短信模板</span>
    </div>
    <div class="line"></div>
    <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加模板
    </Button>
    <sms-template-modal
      @template-valid="handleEdit">
    </sms-template-modal>
    <confirm-modal
      @confirm-valid="handleConfirm">
    </confirm-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TablePage from '_c/table-page'
import SmsTemplateModal from '_c/sms-template-modal'
import ConfirmModal from '_c/confirm-modal'
export default {
  components: {
    SmsTemplateModal,
    ConfirmModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '短信管理短信模板',
      confirmData: {},
      rows: 20,
      loading: true,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '模板标签内容',
          key: 'tag',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '短信内容',
          key: 'content',
          minWidth: 230,
          align: 'center'
        },
        {
          title: '创建时间',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        // {
        //   title: '后缀内容',
        //   key: 'suffix',
        //   // width: 150,
        //   align: 'center'
        // },
        {
          title: '状态',
          key: 'state',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.state === 0 ? '未审核' : params.row.state === 1 ? '审核通过' : params.row.state === 2 ? '审核未过' : '-')
          }
        },
        {
          title: '备注',
          key: 'des',
          minWidth: 250,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.des ? params.row.des : '-')
          }
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 196,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '编辑'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleDelete(params.row.id)
                  }
                }
              }, '删除')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      templateList: state => state.smsTemplate.templateList,
      dataCount: state => state.smsTemplate.templateDataCount,
      historyData: state => state.smsTemplate.templateHistoryData,
      pageSize: state => state.smsTemplate.templatePageSize
    })
  },
  methods: {
    ...mapActions([
      'getSmsTemplateList',
      'getSmsTemplateCurrentRow',
      'editSmsTemplateData',
      'deleteSmsTemplateData',
      'updateModalState',
      'updateConfirmModalState',
      'changeSmsTemplatePage',
      'changeSmsTemplatePageSize'
    ]),
    handleClick (id, row) {
      // console.log(row)
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getSmsTemplateCurrentRow(newRow)
      this.updateModalState(true)
    },
    handleEdit (playload, id) {
      // console.log(playload)
      this.confirmData = playload
      this.updateModalState(false)
      this.updateConfirmModalState(true)
    },
    async handleConfirm () {
      this.loading = true
      const resp = await this.editSmsTemplateData(this.confirmData)
      if (resp.data.state === '000') {
        this.$Message.success('编辑成功')
        this.getSmsTemplateList({
          page: this.page,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateConfirmModalState(false)
    },
    async handleDelete (id) {
      this.loading = true
      const resp = await this.deleteSmsTemplateData({ id: id })
      if (resp.data.state === '000') {
        this.$Message.success('删除成功')
      } else {
        this.$Message.error('删除失败')
      }
      this.changePage(this.page)
      this.getSmsTemplateList({
        page: this.page,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeSmsTemplatePage(index)
      this.getSmsTemplateList({
        page: index,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeSmsTemplatePageSize(value)
      this.changePage(this.page)
      this.getSmsTemplateList({
        rows: value
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getSmsTemplateList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
